﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.DAL;
using WineStore.Models;
using System.Data;
using PagedList;
using PagedList.Mvc;
using System.Data.Entity;

namespace WineStore.Controllers
{

    public class OrderController : Controller
    {
        // GET: /Order/ShowOrder/
        public ViewResult ShowOrder(string currentFilter, string searchString, int? page)
        {
            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = searchString;

            using (var db = new WineDbContext())
            {
                var orders = db.Orders.Include(o => o.Customer).Where(o => o.Status == 1).OrderByDescending(o => o.OrderId);

                if (!String.IsNullOrEmpty(searchString))
                {
                    orders = orders.Where(o => o.Customer.FullName.ToUpper().Contains(searchString.ToUpper())).OrderByDescending(o => o.OrderId);
                }

                int pageSize = 2;
                int pageNumber = (page ?? 1);

                // Thiet lap thuoc tinh khi phan trang
                PagedListRenderOptions pro = new PagedListRenderOptions();
                pro.MaximumPageNumbersToDisplay = 5;
                pro.LinkToLastPageFormat = "Last";
                pro.DisplayLinkToLastPage = true;
                pro.LinkToFirstPageFormat = "First";
                pro.DisplayLinkToFirstPage = true;
                pro.LinkToNextPageFormat = ">>";
                pro.DisplayLinkToNextPage = true;
                pro.LinkToPreviousPageFormat = "<<";
                pro.DisplayLinkToPreviousPage = true;

                ViewBag.Pro = pro;

                return View(orders.ToPagedList(pageNumber, pageSize));
            }
        }

        // GET: /Order/ShowOrderByStatus/
        public ViewResult ShowOrderByStatus(int? status, string currentFilter, string searchString, int? page)
        {
            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = searchString;
            ViewBag.Status = status;
            ViewBag.StatusDes = null;
            if (status == 1)
            {
                ViewBag.StatusDes = "Pending";
            }
            else if (status == 2)
            {
                ViewBag.StatusDes = "Active";
            }
            else if (status == 3)
            {
                ViewBag.StatusDes = "Finish";
            }

            using (var db = new WineDbContext())
            {
                var orders = db.Orders.Include(o => o.Customer).Where(o => o.Status == status).OrderByDescending(o => o.OrderId);

                if (!String.IsNullOrEmpty(searchString))
                {
                    orders = orders.Where(o => o.Customer.FullName.ToUpper().Contains(searchString.ToUpper())).OrderByDescending(o => o.OrderId);
                }

                int pageSize = 50;
                int pageNumber = (page ?? 1);

                // Thiet lap thuoc tinh khi phan trang
                PagedListRenderOptions pro = new PagedListRenderOptions();
                pro.MaximumPageNumbersToDisplay = 5;
                pro.LinkToLastPageFormat = "Last";
                pro.DisplayLinkToLastPage = true;
                pro.LinkToFirstPageFormat = "First";
                pro.DisplayLinkToFirstPage = true;
                pro.LinkToNextPageFormat = ">>";
                pro.DisplayLinkToNextPage = true;
                pro.LinkToPreviousPageFormat = "<<";
                pro.DisplayLinkToPreviousPage = true;

                ViewBag.Pro = pro;

                return View(orders.ToPagedList(pageNumber, pageSize));
            }
        }

        // GET: /Orders/DeleteOrders/
        public ActionResult DeleteOrder(int id)
        {
            Order order = null;
            ViewBag.CustomerName = null;
            using (var db = new WineDbContext())
            {
                order = db.Orders.Find(id);
                ViewBag.CustomerName = order.Customer.FullName;
                if (order == null)
                {
                    return HttpNotFound();
                }
            }
            return View(order);
        }

        // POST: /Orders/DeleteOrders/
        [HttpPost, ActionName("DeleteOrder")]
        public ActionResult DeleteConfirmed(int id)
        {
            Order order = null;
            try
            {
                using (var db = new WineDbContext())
                {
                    order = db.Orders.Find(id);
                    db.Entry(order).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("ShowOrder", "Order");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(order);
        }

        // GET: /Order/ChangeStatus/
        public ActionResult ChangeStatus(int id, int status, string currentFilter)
        {
            Order order = null;
            try
            {
                using (var db = new WineDbContext())
                {
                    order = db.Orders.Find(id);
                    order.Status = status+1;
                    db.Entry(order).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("ShowOrderByStatus", "Order", new { status = status, currentFilter = currentFilter });
                }

            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(order);
        }


        // GET: /Order/ActiveOrder/
        public ActionResult ActiveOrder(int id, string currentFilter)
        {
            Order order = null;
            try
            {
                using (var db = new WineDbContext())
                {
                    order = db.Orders.Find(id);
                    order.Status = 2;
                    db.Entry(order).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("ShowOrder", "Order", new {currentFilter = currentFilter });
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(order);
        }


        // Autocomplete for textbox search 
        [HttpGet]
        public ActionResult Autocomplete(string term)
        {
            using (var db = new WineDbContext())
            {
                var customerNames = from c in db.Customers
                                    select c.FullName;
                string[] items = customerNames.ToArray();

                var filteredItems = items.Where(
                    item => item.IndexOf(term, StringComparison.InvariantCultureIgnoreCase) >= 0
                    );
                return Json(filteredItems, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
